{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第三方模块和测试程序命令\n",
    "\n",
    "- 一个 .Py 文件称之为一个模块 (Module)\n",
    "- 好处：\n",
    "  - 便于代码维护，把很多函数放到不同文件，一个 .py 文件 的 代码数量少\n",
    "  - 一个模块可以被其他地方引用(import)，代码不必从零写起 \n",
    "  - 使用模块还可以避免函数名和变量名冲突。相同名字的函数数和变量完全可以分别存在不同的模块中\n",
    "  \n",
    "- 包 包括很多模块，模块是最小的包"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第三方模块的安装与使用\n",
    "### 模块导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "#调用里面的成员： pd.DataFrame()\n",
    "\n",
    "# 导入模块中的某个成员，使用时可直接调用\n",
    "from os import getcwd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'c:\\\\Users\\\\illus\\\\OneDrive\\\\GIT\\\\data-analysis\\\\lecture-python\\\\doc\\\\1-python-basic'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getcwd()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 自建包\n",
    "\n",
    "- 生成一个文件夹，里面包含很多.py文件，需要有一个 __init__.py 的空文件， python 才可以识别是包\n",
    "\n",
    "- 导入方法：from 包名.文件名(py文件) import 某个方法 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from data_scripts.cbond_script import cbond_update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from .cbond_script import a\n",
    "# from ..cbond_script import a\n",
    "# from ...cbond_script import \n",
    "\n",
    "# 一个点表示当前目录，2个点是上一级\n",
    "# 每多一个点代表目录向上移动一层"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 安装第三方模块 \n",
    "\n",
    "- 模块分 内置模块 和第三方模块 （需要先安装）\n",
    "\n",
    "- 安装第三方模块\n",
    "\n",
    "  - pip install pandas\n",
    "\n",
    "#### 换源\n",
    "\n",
    "- 国内镜像源\n",
    "  - 阿里云： https://mirrors.aliyun.com/pypi/simple/\n",
    "  - 中国科技大学： https://pypi.mirrors.ustc.edu.cn/simple/\n",
    "  - 豆瓣(douban)： https://pypi.douban.com/simple/\n",
    "  - 清华大学：https://pypi.tuna.tsinghua.edu.cn/simple/\n",
    "\n",
    "- 临时使用：\n",
    "  - `pip insta11 包名字 -i 镜像源连接`\n",
    "  - i.e. `pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/s`\n",
    "\n",
    "- 永久修改\n",
    "  - Windows\n",
    "    - 在当前用户目录下创建pip文件夹，例如：`C:\\users\\xxx\\pip`\n",
    "    - 在该文件夹中新建pip.ini文件，\n",
    "    - 在该文件中写入以下内容\n",
    "        ```\n",
    "        [global]\n",
    "        index-url\n",
    "        https://pypi.tuna.tsinghua.edu.cn/simple/\n",
    "        [install]\n",
    "        trusted-host pypi.tuna.tsinghua.edu.cn\n",
    "        ```\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 测试或直接运行文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在python中，如果.py文件是单独运行，__name__=\"__main__\" \n",
    "# 如果.py文件被其它模块引入调用(import)的时候就不等于\n",
    "\n",
    "def func1():\n",
    "    pass\n",
    "\n",
    "def func2():\n",
    "    pass\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    '''\n",
    "    写测试代码,给我们自己调用测试\n",
    "    '''\n",
    "    pass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
